<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <ul>
        <!-- v-for一定要加key -->
        <!-- key的口诀: 有id用 id,没id唯一, 没id用索引 -->
        <li v-for="(item, index) in list" :key="item.id">
          <input />
          <button @click="list.splice(index, 1)">删除</button>
        </li>
      </ul>

      <h1>总年龄: {{totalAge}}</h1>
      <button @click="totalAge = 100">修改年龄</button>
    </div>

    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          list: [
            { id: 1, name: "张三", age: 18 },
            { id: 2, name: "李四", age: 29 },
            { id: 3, name: "王五", age: 30 },
          ],
        },
        methods: {},
        computed: {
          // 简写
          // totalAge() {
          //   return this.list.reduce((pre, item) => {
          //     return pre + item.age;
          //   }, 0);
          // },

          // 完整写法
          totalAge: {
            get() {
              return this.list.reduce((pre, item) => {
                return pre + item.age;
              }, 0);
            },
            set(xxx) {
              console.log("xxx  ----->  ", xxx);
            },
          },
        },
      });
    </script>
  </body>
</html>
